//
//  Event.swift
//  YOKO
//
//  Created by 鲁浩 on 16/5/10.
//  Copyright © 2016年 15Tec. All rights reserved.
//

import Foundation
import RealmSwift

class Event: Object {
    
    dynamic var localId: Int = Int(NSDate().timeIntervalSince1970 * 1000)
    
    override static func primaryKey() -> String? {
        return "localId"
    }
    
    dynamic var uuid: String = NSUUID().UUIDString
    
    dynamic var anyoneCanAddSelf = false
    
    dynamic var color: String = ""
    
    dynamic var createTime: NSDate = NSDate()
    
    dynamic var timeBegin: NSDate = NSDate()
    
    dynamic var timeEnd: NSDate = NSDate()
    
    dynamic var descriptions: String = ""
    
    dynamic var etag: String = ""
    
    dynamic var guestCanInviteOthers = false
    
    dynamic var guestCanModify = false
    
    dynamic var guestCanSeeOtherGuests = false
    
    dynamic var deleted = false
    
    dynamic var isWholeDayEvent = false
    
    dynamic var repeatRULR = ""
    
    dynamic var repeatEXRULE = ""
    
    dynamic var htmlLink: String = ""
    
    dynamic var icalUid: String = ""
    
    dynamic var wholeDayEvent = false
    
    dynamic var location: String = ""
    
    dynamic var locked = false
    
    dynamic var privateCopy = false
    
    dynamic var property: String = ""
    
    dynamic var reminders: String = "0"
    
    dynamic var repeatCount = 0
    
    dynamic var repeatDateEnd: NSDate = NSDate()
    
    dynamic var repeatDateStart: NSDate = NSDate()
    
    dynamic var repeatFreq = 0
    
    dynamic var repeatInterval = 0
    
    dynamic var repeatValue = 0
    
    dynamic var repeatExDate: String = ""
    
    dynamic var repeatRDate: String = ""
    
    dynamic var sequence = 0
    
    // 0 represents delete locally
    dynamic var status = 1
    
    dynamic var summary: String = ""
    
    dynamic var timezone: String = ""
    
    dynamic var transparency = false
    
    dynamic var type: Int = 0
    
    dynamic var updateTime: NSDate = NSDate()
    
    dynamic var calendarId: String = ""
    
    dynamic var version: Int = -1
    
    dynamic var visibility = 0
    
    dynamic var sync_block: Int = 0
    
    func getBarType(dateBegin: NSDate, dateEnd: NSDate) -> BarType {
        if (getEventType() >= 1) {
            return BarType.BarNone
        } else {
            if (self.timeBegin.isInSameDayAsDate(dateBegin)) {
                return BarType.BarStart
            } else if (self.timeEnd.isInSameDayAsDate(dateBegin)) {
                return BarType.BarEnd
            } else {
                return BarType.BarMiddle
            }
        }
    }
    
    func getCircleIconType(dateBegin: NSDate, dateEnd: NSDate) -> CircleIconType {
        if (self.wholeDayEvent) {
            return CircleIconType.FillWithColor
        }
        if !self.timeBegin.isInSameDayAsDate(self.timeEnd) {
            return CircleIconType.HalfFillWithColor
        }
        if self.timeBegin == self.timeEnd {
            return CircleIconType.StrokeWithLine
        }
        if self.timeBegin != self.timeEnd {
            return CircleIconType.HalfFillWithColor
        }
        return CircleIconType.StrokeWithLine
    }
    
    func isShowTitle(dateBegin: NSDate, dateEnd: NSDate) -> Bool {
        if (timeEnd >= dateEnd && dateEnd.weekday == 1) {
            return true
        }
        if (dateEnd.isInSameDayAsDate(NSDate.theNextDays(self.timeEnd, day: 1))) {
            return true
        }
        if (getEventType() > 0) {
            return true
        }
        return false
    }
    
    func getColorType() -> Int {
        return type
    }
    
    func getEventType() -> Int {
        // 0 : 跨天
        // 1 : 全天
        // 2 : 点事件
        // 3 : 当天事件
        if !self.timeBegin.isInSameDayAsDate(self.timeEnd) {
            return 0
        }
        if self.wholeDayEvent {
            return 1
        }
        if self.timeBegin == self.timeEnd {
            return 2
        }
        return 3
    }
    
    static func compareDates(event0: Event, event1: Event) -> Bool {
        let type0 = event0.getEventType()
        let type1 = event1.getEventType()
        if type0 != type1 {
            return type0 < type1
        }
        if type0 == 0 {
            return event0.timeBegin < event1.timeBegin || (event0.timeBegin == event1.timeBegin && event0.timeEnd >= event1.timeEnd)
        }
        if type0 == 1 {
            return true
        }
        return event0.timeBegin < event1.timeBegin || (event0.timeBegin == event1.timeBegin && event0.timeEnd > event1.timeEnd)
    }
    
}
